package client;
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.util.ArrayList;
import java.util.Vector;
import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import client.MaVue.ConnexionListener;


public class MesInformations extends JDialog{
	
/***************** Définitions ****************************/
		//Panel
	  	private JPanel informations = new JPanel();
   	  
	  	//Mes Boutons
	  	private JButton boutonOK = new JButton("OK");
	  	private JButton boutonAjouter= new JButton("Ajouter");
	  	private JButton boutonAnnuler = new JButton("Annuler");

	  	//Modele
	  	MonModele jeSuisTonModele = new MonModele();

	  	//ArrayList pour la création de n nouveaux champs
	  	private ArrayList<JComboBox> combo = new ArrayList();
	  	private ArrayList<JPanel> all = new ArrayList();
	  	private ArrayList<JTextField> attr = new ArrayList(); 
	  	private JTextField nom = new JTextField();
	  	
	  	private String monNom;
	  	private Vector mesAttr = new Vector();
	  	private Vector mesTypes = new Vector();
	  	private Vector maClasseAttr = new Vector();
	  	
	  	//Champs comboBox et JTextField
	  	JComboBox e = new JComboBox();
	  	JTextField a = new JTextField();
	  	
	   
	  	//Erreur
	  	private JOptionPane erreur = new JOptionPane();
	  	
	  	//Compteur du nombre d'attributs
	  	int compteur=0;
	  	boolean ok = false;
  
/******************** Connexion ******************************/
	  public MesInformations(int nb){
		  setModal(true);
		  compteur =nb;
		  this.setTitle("Informations");
		  this.setSize(300, 300);
		  this.setLocationRelativeTo(null);
 
		  creerClasse();
		           
		  }
  
	  public void creerClasse(){
		  JLabel nomLabel = new JLabel("Nom de la classe");
		  nom.setPreferredSize(new Dimension(110, 23));
		  combo= new ArrayList();
		  informations.setLayout(new FlowLayout());
		  informations.add(nomLabel);
		  informations.add(nom);
		  
		  
		  for(int i=0; i<compteur; i++){
			  combo.add(new JComboBox());
			  attr.add(new JTextField());
			  
			  combo.get(i).setPreferredSize(new Dimension(100, 20));
			  combo.get(i).addItem("String");
			  combo.get(i).addItem("Int");
			  combo.get(i).addItem("Char");
			  attr.get(i).setPreferredSize(new Dimension(110, 23));
			  
			  
			  JPanel ligne1 = new JPanel();
			  ligne1.setLayout(new FlowLayout());
			  ligne1.add(combo.get(i));
			  ligne1.add(attr.get(i));
			  informations.add(ligne1);
		  }
		  
		  informations.add(boutonOK);
		  boutonOK.addActionListener((new OkClasseListener()));
		  informations.add(boutonAjouter);
		  boutonAjouter.addActionListener((new AjouterListener()));
		  informations.add(boutonAnnuler);
		  boutonAnnuler.addActionListener((new AnnulerListener()));
		  
		  
		  
		  this.setContentPane(informations);
		  this.setVisible(true);  
	  }
	  
	  public MesInformations(Vector classeAttr, int nb){
		  setModal(true);
		  maClasseAttr=classeAttr;
		  compteur =nb;
		  this.setTitle("Informations");
		  this.setSize(300, 250);
		  this.setLocationRelativeTo(null);
 
		  creerDoc();  
		  }
	  
	  public void creerDoc(){ 
		  informations.setLayout(new FlowLayout());		  
		  
		  for(int i=0; i<compteur; i++){
			  JPanel ligne1 = new JPanel();
			  
			  JLabel type = new JLabel((String) maClasseAttr.get(i));
			  attr.add(new JTextField());
			  attr.get(i).setPreferredSize(new Dimension(110, 23));
			  ligne1.setLayout(new FlowLayout());
			  ligne1.add(type);
			  ligne1.add(attr.get(i));
			  informations.add(ligne1);
		  }
		  
		  informations.add(boutonOK);
		  boutonOK.addActionListener((new OkDocListener()));
		  informations.add(boutonAnnuler);
		  boutonAnnuler.addActionListener((new AnnulerListener()));
		  
		  
		  
		  this.setContentPane(informations);
		  this.setVisible(true);  
	  }
	  
	  class OkDocListener implements ActionListener {
			
			public void actionPerformed(ActionEvent arg0){				
				if(verifDonnee()){
					for(int i=0; i<compteur;i++){
						mesAttr.add(attr.get(i).getText());
						dispose();
					}
				}
				else{
					//erreur.showMessageDialog(null, "Champs invalides !", "C'est raté !",JOptionPane.ERROR_MESSAGE);
				}
			}
	  }
	  
	  class OkClasseListener implements ActionListener {
			
			public void actionPerformed(ActionEvent arg0){				
				if(verifDonnee()){
					monNom=nom.getText();
					for(int i=0; i<compteur;i++){
						mesAttr.add(attr.get(i).getText());
						mesTypes.add(combo.get(i).getSelectedItem());
						dispose();
					}
				}
				else{
					//erreur.showMessageDialog(null, "Champs invalides !", "C'est raté !",JOptionPane.ERROR_MESSAGE);
				}
			}
	  }
	  
	  class AnnulerListener implements ActionListener {
			
			public void actionPerformed(ActionEvent arg0){				
				dispose();
			}
	  }
	  
	  class AjouterListener implements ActionListener {
			
			public void actionPerformed(ActionEvent arg0){	
				compteur++;
				System.out.println("Ajout "+compteur);
				informations.removeAll();
				creerClasse();
				informations.updateUI();
			}
	  }
	  
	  
	  public boolean verifDonnee(){
		  boolean resu = true;
		  
		  for(int i=0; i<compteur;i++){
			  if(attr.get(i)==null)
				  return false;
		  }

		  return resu;
	  }
	  
	  public Vector getAttr(){
		  return mesAttr;
	  }
	  public Vector getTypee(){
		  return mesTypes;
	  }
	  public String getNom(){
		  return monNom;
	  }
	  
	  public int getCompteur(){
		  return compteur;
	  }
	  
	  public boolean isOK(){
		  return ok;
	  }
	  
}

	  
	  